﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Client;
using Cn.Cellula.Model.Security;
using Cn.Edwon.Web;
using System.Reflection;
using Cn.Cellula.Biz;

namespace Cn.Cellula.Website.Admin.User {
    public partial class EditPwd : PageSupport {
        IUserBiz biz = BizFactory.Get<IUserBiz>();
        String userKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            userKey = (Request.QueryString["id"] == null) ? this.Actor.ID : Request.QueryString["id"];
            if (!Page.IsPostBack) {
                FillForm();
            }
        }

        private void FillForm() {
            try {
                VSysUserEO user = biz.Load<VSysUserEO>(userKey);
                this.lab_LoginName.Text = user.LoginName;
                this.lab_UserName.Text = user.UserName;
                this.btn_Save.Enabled = PowerBiz.UserEdit(this.Actor, user).Editable;
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "装载用户数据时错误");
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e) {
            String oldpwd = this.txt_OldPwd.Text.Trim();
            String newpwd = this.txt_NewPwd.Text.Trim();
            String newpwd2 = this.txt_NewPwd2.Text.Trim();
            if (newpwd.Equals(newpwd2)) {
                try {
                    TSysUserEO user = biz.Load<TSysUserEO>(userKey);
                    if (user.UserKey == this.Actor.ID) {
                        if (oldpwd != newpwd) {
                            oldpwd = ClientBiz.Md5(oldpwd);
                            newpwd = ClientBiz.Md5(newpwd);
                            int bk = BizFactory.Get<IUserBiz>().ChangePassword(userKey, oldpwd, newpwd);
                            if (bk == 1) {
                                this.lab_Info.Text = TxtDef.User.PwdNotMatch;
                                return;
                            } else if (bk == 2) {
                                this.lab_Info.Text = "更新密码数据时出现错误";
                                return;
                            }
                            BizFactory.Get<ILogBiz>().LogUpdate("密码", user.LoginName, this.Actor, Request);
                        }
                    } else { //管理员修改用户密码
                        int bk = BizFactory.Get<IUserBiz>().ResetPassword(userKey, newpwd);
                        if (bk == 2) {
                            this.lab_Info.Text = "更新密码数据时出现错误";
                            return;
                        }
                        BizFactory.Get<ILogBiz>().Info("管理员修改了"+user.LoginName+"的密码", this.Actor, Request);
                    }
                    this.lab_Info.Text = TxtDef.SUCCEED;
                } catch (Exception ex) {
                    log.Error("更新用户资料时出现错误:" + ex.Message);
                    SiteInfo.Send(Page, TxtDef.FAILED, "更新用户资料时出现错误");
                }
            } else {
                this.lab_Info.Text = TxtDef.User.PwdNotMatch;
            }
        }
    }
}
